/**
 * describe:
 *
 * @author helloworldyu
 * @data 2018/1/5
 */
/**
 * Builder模式，用于大量可选参数对象的构建(大于4个或内部属性经常扩展)
 * @author qbg
 * 不可变类
 */
public class NutritionFacts {
	private final int servings;//每份含量 必需
	private final int calories;//卡路里 可选
	private final int fat;//脂肪 可选
	private final int sodium;//钠 可选
	private final int carbohydrate;//碳水化合物 可选

	public static class Builder{
		//必需参数
		private final int servings;
		//可选参数
		private int calories = 0;
		private int fat = 0;
		private int sodium = 0;
		private int carbohydrate = 0;

		public Builder(int servings){
			this.servings = servings;
		}

		public Builder calories(int cal){
			calories = cal;
			return this;
		}
		public Builder fat(int fat){
			this.fat = fat;
			return this;
		}
		public Builder sodium(int sod){
			this.sodium = sod;
			return this;
		}
		public Builder carbohydrate(int car){
			this.carbohydrate = car;
			return this;
		}

		public NutritionFacts build(){
			return new NutritionFacts(this);
		}
	}
	/*
	 *私有化构造器，创建该对象只能通过构建器
	 */
	private NutritionFacts(Builder builder){
		servings = builder.servings;
		calories = builder.calories;
		fat = builder.fat;
		sodium = builder.sodium;
		carbohydrate = builder.carbohydrate;
	}

	public void print(){
		System.out.println("servings:"+servings);
		System.out.println("calories:"+calories);
		System.out.println("fat:"+fat);
		System.out.println("sodium:"+sodium);
		System.out.println("carbohydrate:"+carbohydrate);
	}
	public static void main(String[] args) {
		NutritionFacts cocaCola = new NutritionFacts.Builder(600).calories(20).carbohydrate(300).build();
		cocaCola.print();
	}
}